/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.general.bc;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.general.be.TipoUsuario;
import org.cip.esymons.general.be.UserExtMenu;

/**
 *
 * @author ADMINISTRADOR
 */
@Stateless
public class UserExtMenuFacade implements UserExtMenuFacadeLocal {
    @PersistenceContext(unitName = "org.cip.esymons-ejbPU")
    private EntityManager em;

    public void create(UserExtMenu userExtMenu) {
        em.persist(userExtMenu);
    }

    public void edit(UserExtMenu userExtMenu) {
        em.merge(userExtMenu);
    }

    public void remove(UserExtMenu userExtMenu) {
        em.remove(em.merge(userExtMenu));
    }

    public UserExtMenu find(Object id) {
        return em.find(UserExtMenu.class, id);
    }

    public List<UserExtMenu> findAll() {
        return em.createQuery("select object(o) from UserExtMenu as o order by o.tusCodigo.tusNombre, o.uemOrden ").getResultList();
    }

    public List<UserExtMenu> menuPorUsuario(TipoUsuario tipoUsuario) {
        try{
            return em.createQuery("select object(o) from UserExtMenu as o where o.tusCodigo =?1 order by o.uemOrden").setParameter(1, tipoUsuario).getResultList();
        }catch(Exception ex){
            return null;
        }
    }

    public List<UserExtMenu> menuPorUsuarioPorTipo(Integer tipo) {
        try{
        return em.createQuery("select object(o) from UserExtMenu as o where o.tusCodigo.tusCodigo =?1 order by o.uemOrden").setParameter(1, tipo).getResultList();
        }catch(Exception ex){
            return null;
        }
    }

    public List<UserExtMenu> menuPorUsuarioPorTipoUser(String tipo) {
        try{
        return em.createQuery("select object(o) from UserExtMenu as o where o.tusCodigo.tusNombre like '"+tipo+"' order by o.uemOrden").getResultList();
        }catch(Exception ex){
            return null;
        }
    }

    public List<UserExtMenu> findRange(int[] range) {
        Query q = em.createQuery("select object(o) from UserExtMenu as o");
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        return ((Long) em.createQuery("select count(o) from UserExtMenu as o").getSingleResult()).intValue();
    }

}
